package com.newtouch.bxzs.business.message.websocket.controller;

import com.newtouch.bxzs.business.message.websocket.constant.Constant;
import com.newtouch.bxzs.business.message.websocket.service.WebsocketService;
import com.newtouch.bxzs.common.base.model.bo.PushMessageBO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * <b>History:</b>
 * <table border="1">
 * <tr>
 * <th>Date</th>
 * <th>Operator</th>
 * <th>Memo</th>
 * </tr>
 * <tr>
 * <td>2021/2/2 15:18</td>
 * <td>tianshangzhu</td>
 * <td>Create</td>
 * </tr>
 * </table>
 *
 * @author tianshangzhu
 * @version 1.0.0
 * @since 1.0.0
 */
@Controller
@Api(tags = "websocketController")
public class WsController {
    @Resource
    WebsocketService webSocketService;

    @ApiOperation("websocket连接测试")
    @MessageMapping(Constant.FORETOSERVERPATH)//@MessageMapping和@RequestMapping功能类似，用于设置URL映射地址，浏览器向服务器发起请求，需要通过该地址。
    @SendTo(Constant.PRODUCER_PATH)//如果服务器接受到了消息，就会对订阅了@SendTo括号中的地址传送消息。
    public void say(PushMessageBO message) throws Exception {
        List<String> users = new ArrayList<>();
        //此处写死只是为了方便测试,此值需要对应页面中订阅个人消息的userId
        users.add("d892bf12bf7d11e793b69c5c8e6f60fb");
        webSocketService.send2Users(users, PushMessageBO.builder().title("后台通知").content("后台发送测试通知").build());
    }

}
